package com.nercel.dsj.gksales.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@EnableSwagger2
@Configuration
@Profile({"dev", "test", "local"})
public class SwaggerConfig implements WebMvcConfigurer {
    @Bean
    public Docket createRestApi() {
        // 配置全局请求参数
        List<Parameter> globalParameters = new ArrayList<>();

        // Content-Type
        ParameterBuilder contentTypeParameterBuilder = new ParameterBuilder();
        contentTypeParameterBuilder.name("Content-Type").description("Content-Type")
                .modelRef(new ModelRef("string")).parameterType("header").defaultValue("application/json")
                .required(false).build(); //header中的ticket参数非必填，传空也可以
        globalParameters.add(contentTypeParameterBuilder.build());

        // token校验
        ParameterBuilder tokenParameterBuilder = new ParameterBuilder();
        tokenParameterBuilder.name("Authorization").description("token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build(); //header中的ticket参数非必填，传空也可以
        globalParameters.add(tokenParameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.nercel.dsj.gksales.api"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(globalParameters);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("营销工具")
                .version("1.0")
                .build();
    }
}
